package com.choice;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


public class enroll_q_multi extends Activity {
	/** Called when the activity is first created. */
	EditText quiz_title_editT, choice_title_editT;
	Button check_btn, cancel_btn, insert_btn;
	ListView listV;
	Intent intent;
	static ArrayList<String> arraylist;
	static ArrayAdapter<String> adapter ;
	static String title;
	static int longcheck_num;
	int arr_count;	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enroll_q_multi);

        Toast.makeText(getApplicationContext(), "답변입력은 최대 4개까지 가능합니다", Toast.LENGTH_SHORT).show();
        
        quiz_title_editT = (EditText) findViewById(R.id.enroll_q_multi_title_editT);
        choice_title_editT = (EditText) findViewById(R.id.enroll_q_multi_list_editT); 
        check_btn = (Button)findViewById(R.id.enroll_q_multi_check_btn);
        cancel_btn = (Button) findViewById(R.id.enroll_q_multi_cancel_btn);
        listV = (ListView) findViewById(R.id.enroll_q_multi_listV);
        arraylist = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(enroll_q_multi.this, 
        		android.R.layout.simple_expandable_list_item_1,arraylist);
//        		R.layout.list_enroll_q_multi,arraylist);
        
        listV.setAdapter(adapter);
        insert_btn = (Button) findViewById(R.id.editT1_insert_btn);
        
        check_btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {   //입력한 정보들 숫자가 맞는지 확인하고 preview로 인텐트하는부분
				// TODO Auto-generated method stub
				arr_count = arraylist.size();				
				title = quiz_title_editT.getText().toString();
				
				if( title!=null && arr_count>=2 ) {  //최소질문 2개이상
					intent = new Intent(enroll_q_multi.this, enroll_preview_multi.class);
					startActivity(intent);
					
				}
				else {
					Toast.makeText(getApplicationContext(), "제목을 입력하세요", Toast.LENGTH_SHORT).show();
				}			
			}
		});
        cancel_btn.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub

				finish();

			}
		});
        
        insert_btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				if(!choice_title_editT.getText().toString().equals("")) {
					arraylist.add(choice_title_editT.getText().toString());
					choice_title_editT.setText("");
					adapter.notifyDataSetChanged();
				     listV.setAdapter(adapter); 
				     listV.setBackgroundResource(R.drawable.uchoice_sub1_1_img4);
				}
				else {
					Toast.makeText(getApplicationContext(), "답변을 입력하세요", Toast.LENGTH_SHORT).show();
				}
				
			}
		});
        
        
        listV.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				longcheck_num = arg2;
				
				new AlertDialog.Builder(enroll_q_multi.this)
				//.setTitle("")
				.setIcon(R.drawable.icon)
				.setMessage("삭제하시겠습니까?")
				.setCancelable(true)
				.setPositiveButton("네", new DialogInterface.OnClickListener() {
					
					
					public void onClick(DialogInterface dialog, int which) {//해당리스트 삭제
						Toast.makeText(enroll_q_multi.this, longcheck_num+"", Toast.LENGTH_LONG).show();
						arraylist.remove(longcheck_num);
						adapter.notifyDataSetChanged();
					     listV.setAdapter(adapter); 
					}
				})
				.setNeutralButton("아니오", new DialogInterface.OnClickListener() {
							
						
						public void onClick(DialogInterface dialog, int which) {	
								
						}
				}) 
				.show();
				
				return true;
			}
		});
		
    }
}
